#include <unistd.h>
#include <iostream>
#include "../../../plcc/thread/Thread.h"
#include "../../../plcc/event_queue/EventQueue.h"
using namespace std;

class MyT1 :public Thread {
 public:
  MyT1(EventQueue<uint32_t> *q) :q_(q) {} 
 
 private:
  virtual void Run () {
    sleep(1);
    for (int i=0; i<4; i++) {
      cout << "add " << i << " " << Thread::CurrentThreadId()<< endl;
      q_->AddEvent(i);
      sleep(1);
    } 
  } 
  EventQueue<uint32_t> *q_;
};

class MyT2 :public Thread {
 public:
  MyT2(EventQueue<uint32_t> *q) :q_(q){} 
 
 private:
  virtual void Run () {
    //sleep(1);
    for (int i=0; i<4; i++) {
      Type<uint32_t> *tmp = q_->GetEvent();
      cout << "get " << tmp->opcode() << " " << Thread::CurrentThreadId()<< endl;
      delete tmp;
      sleep(1);
    } 
  } 
  EventQueue<uint32_t> *q_;
};

int main () {
  EventQueue<uint32_t> *q = new EventQueue<uint32_t>;
  MyT1 *t1 = new MyT1(q);
  MyT2 *t2 = new MyT2(q);


  t1->Start();
  t2->Start();

  t1->Join();
  t2->Join();
  cout << "Joined." <<endl;
  return 0;
}
